package edu.bit.water.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.bit.water.entity.MetaEvaWbalTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;

import edu.bit.water.controller.MetaEvaCtestSumController;
import edu.bit.water.dao.MetaEvaCtestSumDao;
import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaEvaCtestSum;
import edu.bit.water.entity.MetaEvaWuseInfo;

@Service
@Component
public class MetaEvaCtestSumService {

	// 记得加上这个哇，这是告诉全世界，你要开始在这类里面使用日志
	protected static final Logger logger = LoggerFactory.getLogger(MetaEvaCtestSumController.class);

	@Autowired
	public MetaEvaCtestSumDao metaEvaCtestSumDao;
	@Autowired
	public MetaEvaCtestSumAssociationService metaEvaCtestSumAssociationService;
	@Autowired
	public MetaEvaCtestSumService metaEvaCtestSumService;
	private boolean flag = false;// 用来记录实体表字段是否发生改变，true为已改变，false为改变
	private List<Column> list = null;
	private List<MetaEvaCtestSum> listAll = null;
	@Autowired
	private LabelEvaCtestSumAssociationService labelEvaCtestSumAssociationService;

	/**
	 * @return 根据父序号返回所有字段
	 */
	public List<Column> selectColumnByFlevel(int fLevel) {
		return metaEvaCtestSumDao.selectColumnByFlevel(fLevel);
	}
	/**
	 * @return 返回中英文名
	 */
	public List<Column> selectColumnNotByFlevel() {
		return metaEvaCtestSumDao.selectColumnNotByFlevel();
	}
    
    /**
     * @return 返回所有字段
     */
    public List<Column> selectColumn() {
        if (list == null || flag) {
            list = metaEvaCtestSumDao.selectColumn();
            flag = false;
        }
        return list;
    }
	
    /**
     * @return 返回所有字段
     */
    public List<MetaEvaCtestSum> selectColumnAll() {
        if (listAll == null || flag) {
            listAll = metaEvaCtestSumDao.queryMetaEvaCtestSum();
            flag = false;
        }
        return listAll;
    }
    
    /**
     * @return 返回list
     * @throws JsonProcessingException 
     */
    public ArrayList<MetaEvaCtestSum> queryMetaEvaCtestSum() throws JsonProcessingException {
        ArrayList<MetaEvaCtestSum> list = metaEvaCtestSumDao.queryMetaEvaCtestSum();
        return list;
    }
    
    /**
     * @return 返回list
     * @throws JsonProcessingException 
     */
    public MetaEvaCtestSum queryMetaEvaCtestSumByCsName(String csName) throws JsonProcessingException {
        MetaEvaCtestSum metaEvaCtestSum = metaEvaCtestSumDao.queryMetaEvaCtestSumByCsName(csName);
        return metaEvaCtestSum;
    }
    
    /**
     * 查詢同組列數
     * @param faLevel 組號
     * @return 同組列數
     */
    public int queryColumnNumByFaLevel(int faLevel) {
    	return metaEvaCtestSumDao.queryColumnNumByFaLevel(faLevel);  
    }
    

    
    /**
     * @param
     * @return 成功返回1，失败返回0
     */
    public int deleteMetaEvaCtestSum(List<String> enNameList) {
    	try {
    		for (String enName : enNameList) {
    			metaEvaCtestSumDao.deleteMetaEvaCtestSum(enName);
    		}
    		return 1;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		} finally {
			flag = true;
		}
	}

	/**
	 * @param enName 字段英文名称
	 * @return 成功返回1，失败返回0
	 */
	public int deleteMetaEvaCtestSum(String enName) {
		int n = metaEvaCtestSumDao.deleteMetaEvaCtestSum(enName);
		flag = true;
		return n;
	}

	/**
	 * 
	 * @param
	 * @return
	 */
	public Boolean isContainByCsName(String csName) {
		return metaEvaCtestSumDao.queryMetaEvaCtestSumByCsName(csName) != null ? true : false;
	}
	public Map<String, Object> updateMetaEvaCtestSum(MetaEvaCtestSum metaEvaCtestSum) {

		Map<String, Object> modelMap = new HashMap<>();
		int tmp;

		try{
			 tmp = metaEvaCtestSumDao.updateMetaEvaCtestSum(metaEvaCtestSum);
			 if(!metaEvaCtestSum.getOldName().equals(metaEvaCtestSum.getEnName())){


			metaEvaCtestSumAssociationService.updateMetaEvaCtestSumAssociation(metaEvaCtestSum);
			labelEvaCtestSumAssociationService.updateLabelEvaCtestSumAssociation(metaEvaCtestSum);
			 }
			modelMap.put("status", "success");
			flag = true;



		}catch (DuplicateKeyException e){
			if(e.getMessage().contains("CS_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "中文名称已存在，请重新输入");
			}else if(e.getMessage().contains("EN_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "英文名称已存在，请重新输入");
			}
		}



		return modelMap;
	}
	/**
	 *
	 * @return 成功返回1，失败返回0
	 */
	public Map<String, Object> insertMetaEvaCtestSum(MetaEvaCtestSum metaEvaCtestSum) {
		Map<String, Object> modelMap = new HashMap<>();
		List<Column> list = metaEvaCtestSumDao.selectColumn();
		try {
			metaEvaCtestSumDao.insertMetaEvaCtestSum(metaEvaCtestSum);
			metaEvaCtestSumAssociationService.insertMetaEvaCtestSumAssociation(metaEvaCtestSum);
			labelEvaCtestSumAssociationService.insertLabelEvaCtestSumAssociation(metaEvaCtestSum);
			modelMap.put("status", "success");
			flag = true;
		}catch (DuplicateKeyException e){
			if(e.getMessage().contains("CS_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "中文名称已存在，请重新输入");
			}else if(e.getMessage().contains("EN_NAME")) {
				modelMap.put("status", "failed");
				modelMap.put("msg", "英文名称已存在，请重新输入");
			}
		}
		return modelMap;
	}
}
